﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AidBrella.Models;
using System.Collections;

namespace AidBrella.BLL
{
    public class ProjectModel: BaseModel
    {

        public IEnumerable<ProjectSet> GetProjects()
        {
            return _entities.ProjectSet.OrderByDescending(o => o.Created);
        }

        public ProjectSet GetProject(int id)
        {
            return _entities.ProjectSet.Where(o => o.Id == id).FirstOrDefault();
        }
        
        public void AddProject(ProjectSet p, ProjectSiteSet site)
        {
            p.ProjectSiteSet.Add(site);
            _entities.ProjectSet.AddObject(p);           
            SaveChanges();          
        }

        /// <summary>
        /// Returns Available Project Sectors defined in aidbrella db
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ProjectSectorSet> GetProjectSectors()
        {
            return _entities.ProjectSectorSet;
        }

        /// <summary>
        /// Returns a bool if a user was allowed to succesfully "Follow" a project
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool FollowProject(int projectId, int userId)
        {
            bool following = false;
            lock (this)
            {

                var followProject = _entities.UserFollowProject.Where(o => o.UserId == userId && o.ProjectId == projectId);

                if (followProject.Count() == 0)
                {
                    var ufp = new UserFollowProject();
                    ufp.UserId = userId;
                    ufp.ProjectId = projectId;

                    _entities.UserFollowProject.AddObject(ufp);
                    SaveChanges();

                    following = true;
                }
                else if (followProject.Count() > 0)
                    following = true;
            }
            return following;
        }

        /// <summary>
        /// Returns list of projects user has created/joined (has right to access)
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public IEnumerable<ProjectSet> GetProjectsUserIsInvolved(int userId)
        {
            return _entities.ProjectSet.Where(o => o.CreatedBy == userId).OrderByDescending(o => o.Created);
        }

        /// <summary>
        /// Returns list of projects User is following
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public IEnumerable<ProjectSet> GetProjectsUserFollow(int userId)
        {
            var pIds = _entities.UserFollowProject.Where(u => u.UserId == userId);

            return pIds.Where(item => item != null).Select(item1 => _entities.ProjectSet.Where(p => p.Id == item1.ProjectId).FirstOrDefault()).ToList();
        }

        /// <summary>
        /// Returns a bool if a user succesfully "unfollows" from a project
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool UnFollowProject(int projectId, int userId)
        {
            var following = true;
            lock (this)
            {
                if (_entities.UserFollowProject.Any(o => o.UserId == userId && o.ProjectId == projectId))
                {
                    var pr = _entities.UserFollowProject.Where(o => o.UserId == userId && o.ProjectId == projectId).FirstOrDefault();

                    _entities.UserFollowProject.DeleteObject(pr);
                    SaveChanges();
                    following = false;
                }              
                else 
                {
                    following = false;
                }
            }
            return false;
        }

        public void DeleteProject(int id)
        {
            try
            {
                var p = _entities.ProjectSet.Where(o => o.Id == id);
                if (p.Count() == 1)
                {
                    var lo = _entities.ProjectSiteSet.Where(l => l.ProjectSet_Id == id).FirstOrDefault();
                    _entities.ProjectSiteSet.DeleteObject(lo);
                    _entities.ProjectSet.DeleteObject(p.FirstOrDefault());
                    SaveChanges();
                }
                   
            }
            catch (Exception)
            {

                throw;
            }
        }

        public IEnumerable<ProjectFeed> GetProjectFeeds(int projectId)
        {
           return  _entities.ProjectFeed.Where(o => o.ProjectId == projectId).OrderByDescending(d => d.SubmitedOn).ToList();
        }

        public void UpdateStatus(int projectId, string status, int userId)
        {
            var feed = new ProjectFeed
                           {
                               ProjectId = projectId,
                               Feed = status,
                               SubmitedBy = userId,
                               SubmitedOn = DateTime.Now,
                               AllowComment = true
                           };

            _entities.ProjectFeed.AddObject(feed);
            SaveChanges();
        }

        public string GetProjectLogo(int projectId)
        {
            return "../../Content/images/aidbrella-logo.png";
        }

        public void CommentOnFeed(int feedId, string comment, int userId)
        {
            var cmt = new ProjectFeedCommentSet
                          {
                              ComentDate = DateTime.Now,
                              CommentBy = userId,
                              Comment = comment,
                              FeedId = feedId
                          };

            _entities.ProjectFeedCommentSet.AddObject(cmt);
            SaveChanges();
            
        }

        public void JoinProject(int userId, int projectId, Enums.UserRoleType userRoleType)
        {
            var pm = new ProjectMembers
                          {
                              ProjectId = projectId,
                              RoleId =  (int)userRoleType,
                              UserId = userId
                          };

            _entities.ProjectMembers.AddObject(pm);

            var ufp = new UserFollowProject
                          {
                              ProjectId = projectId,
                              StartFollwDate = DateTime.Now,
                              UserId = userId
                          };

            _entities.UserFollowProject.AddObject(ufp);

            SaveChanges();
        }
    }
}